package com.afkt.project.ui.slice;

import com.afkt.project.model.bean.MyListBean;
import com.afkt.project.slice.UiSlice;
import com.afkt.project.ui.adapter.MyListAdapter;
import com.afkt.project.util.HomeUtils;
import com.afkt.project.util.SkipUtils;
import com.dev.utils.app.*;
import com.dev.utils.app.assist.BeepVibrateAssist;
import com.dev.widget.qrcode.core.ZbarView;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;
import ohos.event.notification.NotificationRequest;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.media.audio.AudioManager;
import ohos.media.audio.AudioStreamInfo;
import ohos.media.audio.SoundPlayer;
import ohos.media.camera.CameraKit;
import ohos.media.camera.device.Camera;
import ohos.media.camera.device.CameraStateCallback;
import ohos.media.camera.device.FrameConfig;
import ohos.system.DeviceInfo;
import ohos.vibrator.agent.VibratorAgent;

import java.util.ArrayList;
import java.util.List;

import static com.dev.utils.app.MemoryUtils.*;
import static ohos.media.camera.device.Camera.FrameConfigType.FRAME_CONFIG_PREVIEW;
import static ohos.media.camera.params.Metadata.FlashMode.FLASH_ALWAYS_OPEN;
import static ohos.media.camera.params.Metadata.FlashMode.FLASH_CLOSE;

public class RingtoneSlice extends AbilitySlice {

    private ListContainer listContainer;
    private MyListAdapter mAdapter;

    private VibratorAgent vibratorAgent = new VibratorAgent();
    private SoundPlayer soundPlayer;
    private CameraKit cameraKit;
    private Camera mcamera;
    int notificationId = 1;
    int soundId = 0;
    NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
    NotificationRequest request = new NotificationRequest(notificationId);


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_layout_ringtone);
        initList();
    }

    private void initList() {
        List<MyListBean> list = initData();
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_ringtone_list);
        mAdapter = new MyListAdapter(list, this);
        listContainer.setItemProvider(mAdapter);
        listContainer.setItemClickedListener((listContainer, component, position, l) -> {
            switch (position) {
                //震动
                case 0:
                    BeepVibrateAssist.startTimeVibrate(vibratorAgent, RingtoneSlice.this);
                    break;
                //铃声-播放一小段音频
                case 1:
                    if (soundPlayer == null) {
                        soundPlayer = new SoundPlayer(AudioManager.AudioVolumeType.STREAM_MUSIC.getValue());
                    }
                    // 步骤2：指定音频资源加载并创建短音
                    soundId = soundPlayer.createSound(getContext(), ResourceTable.Media_yinpin);
                    // 步骤3：指定音量，循环次数和播放速度
                    SoundPlayer.SoundPlayerParameters parameters = new SoundPlayer.SoundPlayerParameters();
                    parameters.setVolumes(new SoundPlayer.AudioVolumes());
                    parameters.setLoop(10);
                    parameters.setSpeed(1.0f);
                    soundPlayer.setOnCreateCompleteListener((soundPlayer1, i, i1) -> {
                        // 步骤4：短音播放
                        int result = soundPlayer.play(soundId, parameters);
                    });

                    break;
                //是否存在通知权限
                case 2:
                    final boolean flag = NotificationUtils.isAllowedNotifyNotice();
                    if (flag) {
                        new ToastDialog(getContext()).setText("通知权限已开启！").show();
                    } else {
                        new ToastDialog(getContext()).setText("通知权限未开启！").show();
                    }
                    break;
                //开启通知权限
                case 3:
                    //NoticeUtils.setNotifyNotice();
                    new ToastDialog(getContext()).setText("暂时只支持跳转系统设置").show();
                    startAbility(OpenNetUtils.openNet());
                    break;
                //发布通知消息
                case 4:
                    NotificationUtils.initNotificationSlot();
                    NotificationUtils.publishNotification();
                    new ToastDialog(getContext()).setText("已发布通知消息！").show();
                    break;
                //移除通知消息
                case 5:
                    NotificationUtils.cancelNotification();
                    new ToastDialog(getContext()).setText("已移除通知消息！").show();
                    break;
                //回到桌面
                case 6:
                        Intent intent = HomeUtils.backDesktop();
                        startAbility(intent);

                    break;
                //打开手电筒
                case 7:
                    SkipUtils.startNewAbility(RingtoneSlice.this,"com.ohos.afkt","com.afkt.project.NewTaskQRAbility");
                    break;
                //关闭手电筒
                case 8:
                    SkipUtils.startNewAbility(RingtoneSlice.this,"com.ohos.afkt","com.afkt.project.NewTaskQRAbility");
                    break;
                //是否创建桌面快捷方式
                case 9:
                    new ToastDialog(getContext()).setText("暂不支持").show();
                    break;
                //创建桌面快捷方式
                case 10:
                    new ToastDialog(getContext()).setText("暂不支持").show();
                    break;
                //删除桌面快捷方式
                case 11:
                    new ToastDialog(getContext()).setText("暂不支持").show();
                    break;
                //打印内存信息
                case 12:
                    String getPhoneMemory = getPhoneMemory(getAbilityManager());
                    String phoneAvailMemory = getPhoneAvailMemory(getAbilityManager());
                    String memory = "手机总内存为：" + getPhoneMemory + ";  手机可用内存为" + phoneAvailMemory;
                    printInformation(memory, getContext());
                    break;
                //打印设备信息
                case 13:
                    int width = getDisplayWidthInPx(getContext());
                    int height = getheight(getContext());
                    //分辨率
                    String resolution = getheight(getContext()) + "x" + getDisplayWidthInPx(getContext());
                    //获取手机型号
                    String model = DeviceInfo.getModel();
                    //获取设备版本号码
                    String versionNum = "HUAWEI" + DeviceInfo.getModel();
                    String equipment = "屏幕宽度：" + width + ";  屏幕高度：" + height + ";  设备分辨率：" + resolution + ";  手机型号：" + model + ";  手机版本号码：" + versionNum;
                    printInformation(equipment, getContext());
                    break;
                //打开App设置
                case 14:
                    startAbility(OpenAppSetting.openAppSet());
                    break;
                //打开GPS设置页面
                case 15:
                    startAbility(OpenGpsSetting.setGPS());
                    new ToastDialog(getContext()).setText("暂不支持,目前只能跳转设置页面").show();
                    break;
                //打开网络设置页面
                case 16:
                    startAbility(OpenNetUtils.openNet());
                    break;
                //打开系统设置页面
                case 17:
                    startAbility(OpenNetUtils.openNet());
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        if (soundId != 0 && soundPlayer != null) {
            // 步骤5：停止播放
            soundPlayer.stop(soundId);
            // 步骤6：释放短音资源
            soundPlayer.deleteSound(soundId);
        }

    }

    @Override
    protected void onStop() {
        super.onStop();

        if (soundId != 0 && soundPlayer != null) {
            // 步骤5：停止播放
            soundPlayer.stop(soundId);
            // 步骤6：释放短音资源
            soundPlayer.deleteSound(soundId);
        }
    }

    private List<MyListBean> initData() {
        List<MyListBean> lists = new ArrayList<>();
        lists.add(new MyListBean("震动"));
        lists.add(new MyListBean("铃声-播放一小段音频"));
        lists.add(new MyListBean("是否存在通知权限"));
        lists.add(new MyListBean("开启通知权限"));
        lists.add(new MyListBean("通知消息"));
        lists.add(new MyListBean("移除消息"));
        lists.add(new MyListBean("回到桌面"));
        lists.add(new MyListBean("打开手电筒"));
        lists.add(new MyListBean("关闭手电筒"));
        lists.add(new MyListBean("是否创建桌面快捷方式"));
        lists.add(new MyListBean("创建桌面快捷方式"));
        lists.add(new MyListBean("删除桌面快捷方式"));
        lists.add(new MyListBean("打印内存信息"));
        lists.add(new MyListBean("打印设备信息"));
        lists.add(new MyListBean("跳转到APP设置详情页面"));
        lists.add(new MyListBean("打开GPS设置页面"));
        lists.add(new MyListBean("打开网络设置页面"));

        lists.add(new MyListBean("跳转到系统设置页面"));
        return lists;
    }


}
